package org.ww.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.ww.common.constant.RabbitMqConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: gmall
 * @description:
 * @author: ww
 * @create: 2024-10-28 10:35
 **/

@Configuration
public class RabbitmqConfig {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

//    @RabbitListener(queues = MqConstant.STOCK_DELAY_QUEUE)
//    public void listener(Message message){
//        System.out.println("监听队列消息");
//    }

    @Bean
    public Exchange stockEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange(RabbitMqConstant.STOCK_EVENT_EXCHANGE,true,false);
    }

    @Bean
    public Queue stockDelayQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", RabbitMqConstant.STOCK_EVENT_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","stock.release");
        arguments.put("x-message-ttl",120000);
        return new Queue(RabbitMqConstant.STOCK_DELAY_QUEUE,true,false,false,arguments);
    }

    @Bean
    public Queue stockReleaseStockQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        return new Queue(RabbitMqConstant.STOCK_RELEASE_STOCK_QUEUE,true,false,false);
    }

    @Bean
    public Binding stockDelayBinding(){
        //String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
        return new Binding(RabbitMqConstant.STOCK_DELAY_QUEUE, Binding.DestinationType.QUEUE,RabbitMqConstant.STOCK_EVENT_EXCHANGE,RabbitMqConstant.STOCK_LOCKED_BINDING,null);
    }

    @Bean
    public Binding stockReleaseStockBinding(){
        //String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
        return new Binding(RabbitMqConstant.STOCK_RELEASE_STOCK_QUEUE, Binding.DestinationType.QUEUE,RabbitMqConstant.STOCK_EVENT_EXCHANGE,RabbitMqConstant.STOCK_RELEASE_BINDING,null);
    }
}
